/*
 * Created on Jan 17, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package bmoofxbridge.model;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import org.apache.xmlbeans.XmlException;

import net.ofx.types.x2003.x04.Currency;
import net.ofx.types.x2003.x04.StatementTransaction;
import net.ofx.types.x2003.x04.TransactionEnum;

import com.develop.jawin.COMException;

import bmoofxbridge.BMOParser;

/**
 * @author leonidr
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Transaction {
    public static short[] AccountDataRelevanceDate = new short[] {0,0};
	public static short[] TransactionDate = new short[] {0,2};
	public static short[] StatementDate = new short[] {0,0};
	public static short[] TransactionAmmount = new short[] {1,0};
	public static short[] TransactionBalance = new short[] {1,1};
	public static short[] TransactionAccount = new short[] {2,0};
	public static short[] TransactionRefNumber = new short[] {2,1};
	public static short[] TransactionMemo = new short[] {2,2};
	public static short[] TransactionBank = new short[] {2,3};
	public static short   TransactionAmmountDouble = 0;
	public static short   TransactionBalanceDouble = 1;
	
	protected double ammount;
	protected double balance;
	protected String refNumber;
	protected Date date;
	protected Date statementDate;
	protected String accountId;
	protected String bankName;
	protected String name;
	
	public Transaction(){}
	
	public Transaction(BMOParser parser) {
	    try {
            ammount = parser.getValue(TransactionAmmountDouble);
            balance = parser.getValue(TransactionBalanceDouble);
            refNumber = parser.getValue(TransactionRefNumber).trim();
            date = BMOParser.fromBMODateFormat.parse(parser.getValue(TransactionDate));
            statementDate = BMOParser.fromBMODateFormat.parse(parser.getValue(StatementDate));
            accountId = parser.getValue(TransactionAccount);
            name = parser.getValue(TransactionMemo);
            bankName = parser.getValue(TransactionBank);
            
        } catch (COMException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
	    
	}
	
	public StatementTransaction getCheckingTransaction() throws COMException {
	    StatementTransaction transaction = StatementTransaction.Factory.newInstance();
	    if (getAmmount()<0) {
            transaction.setTRNTYPE(TransactionEnum.PAYMENT);
        }
        else {
            transaction.setTRNTYPE(TransactionEnum.DEP);
        }
	    
	    
	    String dateStr=BMOParser.toOFXDateFormat.format(getDate());
	    //transaction.setCURRENCY(BMOParser.ILS);
	    transaction.setDTPOSTED(dateStr);
	    transaction.setFITID(getId());
	    transaction.setMEMO(getName());
	    transaction.setNAME(getName());
	    transaction.setREFNUM(getRefNumber());
	    transaction.setTRNAMT(String.valueOf(getAmmount()));
	    
        
	    return transaction;
	}

	public String getId(){
		String dateStr=BMOParser.toOFXDateFormat.format(getDate());
		return dateStr+getRefNumber();
	}
    /**
     * @return Returns the accountId.
     */
    public String getAccountId() {
        return accountId;
    }
    /**
     * @return Returns the ammount.
     */
    public double getAmmount() {
        return ammount;
    }
    /**
     * @return Returns the balance.
     */
    public double getBalance() {
        return balance;
    }
    /**
     * @return Returns the bankName.
     */
    public String getBankName() {
        return bankName;
    }
    /**
     * @return Returns the date.
     */
    public Date getDate() {
        return date;
    }
    /**
     * @return Returns the name.
     */
    public String getName() {
        return name;
    }
    /**
     * @return Returns the refNumber.
     */
    public String getRefNumber() {
        return refNumber;
    }
    /**
     * @return Returns the statementDate.
     */
    public Date getStatementDate() {
        return statementDate;
    }
	/**
	 * @param refNumber The refNumber to set.
	 */
	public void setRefNumber(String refNumber) {
		this.refNumber = refNumber;
	}

	public Account getAccount() {
		return new Account(bankName,null,accountId);
	}
}
